from datetime import datetime
from decimal import Decimal

from django.db import transaction
from django_redis import get_redis_connection
from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class CartSKUSerializer(serializers.ModelSerializer):
    """购物车商品数据序列化器"""
    count = serializers.IntegerField(label="数量")

    class Meta:
        model = SKU
        fields = ('id', 'name', 'default_image_url', 'price', 'count')


class OrderSettlementSerializer(serializers.Serializer):
    """
    订单结算数据序列化器
    """
    freight = serializers.DecimalField(label='运费', max_digits=10, decimal_places=2)
    skus = CartSKUSerializer(many=True)


class OrderSerializer(serializers.ModelSerializer):
    """订单保存序列化器类"""

    class Meta:
        model = OrderInfo
        fields = ('order_id', 'address', 'pay_method')
        read_only_fields = ('order_id',)
        extra_kwargs = {

            'address': {
                'write_only': True,
                'required': True,
            },
            'pay_method': {
                'write_only': True,
                'required': True
            }
        }

    def create(self, validated_data):
        """保存提交的订单数据"""
        # 获取收货地址address 和 支付方式pay_method
        address = validated_data['address']
        pay_method = validated_data['pay_method']

        # 组织参数
        # 获取登陆用户

        user = self.context['request'].user

        # 订单编号 格式: 年月日时分秒 + 用户id
        order_id = datetime.now().strftime('%Y%m%d%H%M%S')+'%010d' % user.id

        # 订单商品总数量和总金额
        total_count = 0
        total_amount = Decimal(0)

        # 运费
        freight = Decimal(10)

        # 判断订单状态，哪种支付方式
        status = OrderInfo.ORDER_STATUS_ENUM['UNSEND'] if pay_method == OrderInfo.PAY_METHODS_ENUM['CASH'] else OrderInfo.ORDER_STATUS_ENUM['UNPAID']

        # 2）订单中包含几个商品，需要向订单商品表中添加几条记录。
        redis_conn = get_redis_connection('cart')

        # 从redis中获取用户购物车中被勾选的商品的sku_id    set
        cart_selected_key = 'cart_selected_%s' % user.id

        sku_ids = redis_conn.smembers(cart_selected_key)

        cart_key = 'cart_%s' % user.id

        cart_dict = redis_conn.hgetall(cart_key)

        # 订单对象
        order = None

        with transaction.atomic():
            # 在with语句块中的代码，凡是涉及到数据库操作的，都会放在同一个事务中

            # 设置事物保存点
            sid = transaction.savepoint()

            try:
                # 向订单中添加记录
                order = OrderInfo.objects.create(
                    order_id=order_id,
                    user=user,
                    address=address,
                    total_count=total_count,
                    total_amount=total_amount,
                    freight=freight,
                    pay_method=pay_method,
                    status=status
                )

                for sku_id in sku_ids:
                    # 获取所有的商品的数量count
                    count = int(cart_dict[sku_id])

                    for i in range(3):
                        # 根据sku_id 获取商品信息
                        sku = SKU.objects.get(id=sku_id)

                        if count > sku.stock:
                            transaction.savepoint_rollback(sid)
                            raise serializers.ValidationError("商品库存不足")

                        # 记录商品原始库存
                        origin_stock = sku.stock
                        new_stock = origin_stock - count
                        new_sales = sku.sales + count
                        # update返回更新的行数
                        res = SKU.objects.filter(id=sku_id, stock=origin_stock).update(stock=new_stock, sales=new_sales)

                        if res == 0:
                            if i == 2:
                                # 说明重新尝试了3次，更新仍然失败，直接报下单失败
                                # 回滚事务到sid保存点
                                transaction.savepoint_rollback(sid)
                                raise serializers.ValidationError('下单失败2')
                            # 更新失败，重新进行尝试
                            continue
                        # 向订单商品表中添加一条记录
                        OrderGoods.objects.create(
                            order=order,
                            sku=sku,
                            count=count,
                            price=sku.price
                        )
                        # 累加计算订单商品的总数量和总金额
                        total_count += count
                        total_amount += sku.price * count

                        # 更新成功，跳出循环
                        break

                # 实付款
                total_amount += freight
                # 更新订单记录中商品的总数量和实付款
                order.total_count = total_count
                order.total_amount = total_amount
                order.save()

            except serializers.ValidationError:
                raise

            except Exception as e:
                # 回滚事务到sid保存点
                transaction.savepoint_rollback(sid)
                raise serializers.ValidationError('下单失败')

        # 3.清除redis中对应的购物车记录
        pl = redis_conn.pipeline()
        pl.hdel(cart_key, *sku_ids)
        pl.srem(cart_selected_key, *sku_ids)
        pl.execute()

        # 返回订单对象
        return order
